#pragma once

#include "dsegen.h"
#include "DsegenTypes.h"

namespace dhis
{
	class CEdge
	{
	public:
		CEdge(void);
		~CEdge(void);

		CEdge(const CEdge& source);
		CEdge& operator =(const CEdge& source);

		bool operator==(const CEdge &other) const;

		void Clear();

		void SetVorH(const bool& value);
		bool GetVorH() const;

		void SetTail(const unsigned int& row,const unsigned int& col);
		void GetTail(unsigned int& row,unsigned int& col) const;

		inline unsigned int GetTailCol() const { return m_tailCol; }
		inline unsigned int GetTailRow() const { return m_tailRow; }

		void SetHead(const unsigned int& row,const unsigned int& col);
		void GetHead(unsigned int& row,unsigned int& col) const;

		inline unsigned int GetHeadCol() const { return m_headCol; }
		inline unsigned int GetHeadRow() const { return m_headRow; }

		inline void GetTail(CIndex& idx) const
		{
			idx.m_col = GetTailCol();
			idx.m_row = GetTailRow();
		}

		inline void GetHead(CIndex& idx) const
		{
			idx.m_col = GetHeadCol();
			idx.m_row = GetHeadRow();
		}
	public:
		bool IsConnected(const CEdge& e) const;

		bool GetConnectPoint(const CEdge& e, unsigned int& row, unsigned int& col) const;

	private:
		bool m_vorh;                   // true:horizontal; false:vertical
		unsigned int m_tailCol;
		unsigned int m_tailRow;
		unsigned int m_headCol;
		unsigned int m_headRow;
	};

	//------------------------------------------------------------------------------  
	inline bool existEdge(vector<CEdge>& edges,const CEdge& e)
	{
		vector<CEdge>::iterator itr = std::find(edges.begin(),edges.end(),e);
		return itr != edges.end();
	}

	//------------------------------------------------------------------------------  
	inline vector<CEdge>::iterator findConnectedEdge(vector<CEdge>& edges,const unsigned int& row, const unsigned int& col,bool& tailOrHead)
	{
		for (vector<CEdge>::iterator i = edges.begin(); i != edges.end(); ++i)
		{
			CEdge& e = *i;
			if (row == e.GetTailRow() && col == e.GetTailCol())
			{
				tailOrHead = true;
				return i;
			}
			else if (row == e.GetHeadRow() && col == e.GetHeadCol())
			{
				tailOrHead = false;
				return i;
			}
		}
		return edges.end();
	}


}